package com.mi.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.MyPages;
import com.mi.entity.ResponseMsg;
import com.mi.entity.UserEntity;
import com.mi.service.UserService;
import com.mi.util.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
public class UserController {

    @Reference(check = false)
    UserService userService;

    //会员列表
    @RequestMapping("userList")
    public String userList(UserEntity userEntity, HttpServletRequest request){
        MyPages<UserEntity> userEntityMyPages = userService.queryCustomer(userEntity);
        request.setAttribute("pageInfo",userEntityMyPages);
        request.setAttribute("uname",userEntity.getUname());
        return "userList";
    }
    //删除会员
    @RequestMapping("delUser")
    @ResponseBody
    public ResponseMsg delUser(String uid) {
        ResponseMsg rm = new ResponseMsg();
        String[] cids;
        if (uid.contains(",")) {
            cids = uid.split(",");
        } else {
            cids = new String[]{uid};
        }
        boolean b = userService.delCustomer(cids);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //会员状态更改
    @RequestMapping("updUserStatus")
    @ResponseBody
    public ResponseMsg updCustomerStatus(UserEntity userEntity) {
        ResponseMsg rm = new ResponseMsg();
        boolean b = userService.updCustomerStatus(userEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //跳转到修改会员界面
    @RequestMapping("userEdit")
    public String userEdit(UserEntity userEntity,HttpServletRequest request){
        UserEntity userEntity1 = userService.queryUser(userEntity);
        if(userEntity1 != null){
            request.setAttribute("userPojo",userEntity1);
            return "userEdit";
        }else{
            return "error";
        }
    }

    //修改会员
    @RequestMapping("updUser")
    @ResponseBody
    public ResponseMsg updUser(MultipartFile myfile, UserEntity userEntity, HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String customerImage = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"userIcon";
            File file = new File(customerImage);
            if(!file.exists()){
                file.mkdirs();
            }
            customerImage = customerImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(customerImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                userEntity.setUimage(customerImage.substring(customerImage.lastIndexOf(File.separator,customerImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean b = userService.updCustomer(userEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到添加会员界面
    @RequestMapping("userAdd")
    public String userAdd(){
        return "userAdd";
    }

    //添加会员
    @RequestMapping("addUser")
    @ResponseBody
    public ResponseMsg addUser(UserEntity userEntity){
        ResponseMsg rm = new ResponseMsg();
//        userEntity.setUpass(DigestUtils.md5DigestAsHex(userEntity.getUpass().getBytes()));
        boolean regist = userService.regist(userEntity);
        if (regist) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("添加失败");
        }
        return rm;
    }

    //跳转到修改密码界面
    @RequestMapping("userPassEdit")
    public String userPassEdit(String uid,HttpServletRequest request){
        request.setAttribute("uid",uid);
        return "userPassEdit";
    }

    //修改密码
    @RequestMapping("updUserPass")
    @ResponseBody
    public ResponseMsg  updUserPass(UserEntity userEntity){
        ResponseMsg rm = new ResponseMsg();
//        userEntity.setUpass(DigestUtils.md5DigestAsHex(userEntity.getUpass().getBytes()));
//        userEntity.setOldpass(DigestUtils.md5DigestAsHex(userEntity.getOldpass().getBytes()));
        boolean b = userService.updPass(userEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //会员用户名唯一校验
    @RequestMapping("soleUser")
    @ResponseBody
    public ResponseMsg soleAdmin(String uname){
        ResponseMsg rm = new ResponseMsg();
        UserEntity userEntity = userService.uniqueName(uname);
        if(userEntity!=null && userEntity.getUname()!=null){
            rm.setResult("0000");
        }else{
            rm.setResult("0001");
        }
        return rm;
    }

}
